﻿Imports System.Data.SqlClient
Public Class FormEnrollmentEditor

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub txtSTUDENT_CODE_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSTUDENT_CODE.KeyDown
        If e.KeyCode = Keys.Enter Then
            ShowStudent()
        End If
    End Sub

    Public Sub ShowStudent() 
        Dim cn As New SqlConnection("server=.;uid=sa;pwd=123;database=SchoolDb;")
        cn.Open()
        Dim cmd As New SqlCommand("SELECT * FROM TBL_STUDENT WHERE STUDENT_CODE=N'" & txtSTUDENT_CODE.Text & "'", cn)
        Dim dr = cmd.ExecuteReader()
        Dim dt As New DataTable()
        dt.Load(dr)
        dr.Close()

        If dt.Rows.Count = 0 Then
            MsgBox("No student found!")
        Else
            Me.txtSTUDENT_NAME.Text = dt.Rows(0)("NAME")
            Me.txtSTUDENT_BIRTH_DATE.Text = dt.Rows(0)("BIRTH_DATE")

        End If
    End Sub

    Public Sub LoadSubject()
        Dim cn As New SqlConnection("server=.;uid=sa;pwd=123;database=SchoolDb;")
        cn.Open()

        Dim cmd As New SqlCommand("SELECT * FROM TBL_SUBJECT;", cn)
        Dim dr = cmd.ExecuteReader()

        Dim dt As New DataTable()
        dt.Load(dr)
        dr.Close()

        cboSUBJECT.DisplayMember = "SUBJECT_NAME"
        cboSUBJECT.ValueMember = "SUBJECT_CODE"
        cboSUBJECT.DataSource = dt
    End Sub

    Private Sub FormEnrollmentEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadSubject()
    End Sub

    Private Sub cboSUBJECT_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSUBJECT.SelectedIndexChanged

        Dim cn As New SqlConnection("server=.;uid=sa;pwd=123;database=SchoolDb;")
        cn.Open()

        Dim cmd As New SqlCommand("SELECT PRICE FROM TBL_SUBJECT WHERE SUBJECT_CODE=N'" & cboSUBJECT.SelectedValue & "'", cn)
        txtFEE.Text = cmd.ExecuteScalar()

    End Sub
End Class